from database.db_handler import MysqlHander
from my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import json


'''
产品/业务信息匹配
'''

class BusinessMatch:
    def __init__(self):
        self.db = MysqlHander("config.ini")
        self.file = open("business_match.txt", "w", encoding="utf8")
        self.business = []
        self.stock_business = []
        self.stock = {}
    
    def get_data(self, tag, code):
        url = "https://emweb.securities.eastmoney.com/PC_HSF10/BusinessAnalysis/PageAjax?code=" + tag + code
        http_data = MyHttp.http_json_data(url)
        json_data = json.loads(http_data)
        if "status" in json_data:
            return
        for d in json_data["zygcfx"]:
            if d["MAINOP_TYPE"] == "2":
                self.stock_business.append(d["ITEM_NAME"])

    def write_file_data(self):
        business_map = {}
        #business_set = set(self.business)
        for s in self.business:
            if s in business_map:
                business_map[s] = business_map[s] + 1
            else:
                business_map[s] = 1
        for k,v in business_map.items():
            self.file.write(k + "\t" + str(v) + "\n")
        self.file.close()

    def query_business(self):
        sql = "select Fattribute_name from attribute_data where Frelation_id = 2002;"
        business_set = self.db.query_sql(sql)
        for b in business_set:
            self.business.append(b[0])
    def query_stock(self):
        sql = "select Fcode,Fname,Fexchange_id from entity_gupiao;"
        stock_set = self.db.query_sql(sql)
        for s in stock_set:
            self.stock.[s[0]] = s[2]


    def add_relation(self, code, attr):
        if attr not in self.business:
            return
        sql = "insert into relation_data_02 (Fsrc_code, Fsrc_type, Frelation_id, Fattribute_name, Fcreate_time, Fmodify_time) values "
        sql += "('" + code + "',0,2002,'" + attr + "', now(), now());\n"
        print(sql)
        self.file.write(sql)

    def process(self):
        for s,v in self.stock.items():
            self.stock_business.clear()
            if int(v) == 1:
                self.get_data("SH", s)
            if int(v) == 2:
                self.get_data("SZ", s)
            for b in list(set(self.stock_business)):
                self.add_relation(s, b)
                seg_simple = re.split('[,|;()]+',b)
                for seg in seg_simple:
                    self.add_relation(s, seg)
                seg_list_v1 = jieba.cut(b, cut_all=True)
                for seg in seg_list_v1:
                    self.add_relation(s, seg)
                seg_list_v2 = jieba.cut_for_search(b)
                for seg in seg_list_v2:
                    self.add_relation(s, seg)

    def match(self):
        self.query_business()
        self.query_stock()
        self.process()
        self.file.close()


if __name__ == '__main__':
    code = BusinessMatch()
    code.match()